package com.jz.netthings.security.browser.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jz.netthings.security.utils.AdscmSecurityUtils;
import com.jz.netthings.support.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yyy
 * @ClassName AdscmErrorInterceptor
 * @Description 其他错误拦截器
 * @date 2018/7/27 11:49
 *
 *
 * 没什么卵用。。。。。不用这个了
 */
//@Component
//@Order(Integer.MAX_VALUE)
@Deprecated
public class AdscmErrorInterceptor implements Filter {

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


        chain.doFilter(request,response);

        if (response instanceof HttpServletResponse){
            HttpServletResponse resp = (HttpServletResponse)response;
            HttpServletRequest req = (HttpServletRequest)request;

            if(resp.getStatus()==500){
                req.setAttribute("statusCode","500");
                req.setAttribute("desc","服务器异常");

                req.getRequestDispatcher(SecurityConstants.DEFAULT_ACCESS_DENIED_URL).forward(req,resp);
            }else if(resp.getStatus()==404){

                AdscmSecurityUtils.getInstance().printMsgByResponse("404","资源不存在",resp,objectMapper);

            }
        }
    }

    @Override
    public void destroy() {

    }
}
